package com.sun.talk.utils

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException
import java.util.*
import com.baomidou.mybatisplus.core.toolkit.StringPool
import java.util.ArrayList
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.generator.AutoGenerator
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import java.util.ResourceBundle




object CodeGenerator {

    /**
     * 获取控制台上的内容
     * @param tip 控制台输入的内容
     * @return
     */
    fun scanner(tip: String): String {
        val scanner = Scanner(System.`in`)
        val stringBuilder = StringBuilder()
        stringBuilder.append("请输入$tip:")
        println(stringBuilder.toString())
        if (scanner.hasNext()) {
            val ipt = scanner.next()
            if (ipt.isNotEmpty()) {
                //输入的不是空就返回
                return ipt
            }
        }
        throw MybatisPlusException("请输入正确的$tip! ")
    }
    @JvmStatic
    fun main(args: Array<String>) {
        val resource = ResourceBundle.getBundle("myBatisPlus")
        val outPutDir = resource.getString("outputDir")
        var fileOverride = false
        if ("true" == resource.getString("fileOverride")) {
            fileOverride = true
        }
        val url = resource.getString("url")
        val driverClass = resource.getString("driverClass")
        val userName = resource.getString("userName")
        val passWord = resource.getString("passWord")
        val setParent = resource.getString("setParent")
        val mapperPath = resource.getString("mapperPath")

        //代码生成器
        val mpg = AutoGenerator()

        //全局配置
        val gc = GlobalConfig()
        //生成的文件输出到哪
        val projectPath = System.getProperty("user.dir")
        gc.outputDir = projectPath + outPutDir
        //默认主键生成策略为uuid
        // AUTO(0),
        //    NONE(1),
        //    INPUT(2),
        //    ID_WORKER(3),
        //    UUID(4),
        //    ID_WORKER_STR(5)
        gc.idType = IdType.ID_WORKER_STR
        gc.isOpen = false
        gc.isFileOverride = fileOverride
        gc.author = "SUN"
        gc.isBaseColumnList = true
        gc.isBaseResultMap = true
        gc.isKotlin = true

        gc.entityName = "%sEntity"
        gc.mapperName = "%sDao"
        gc.serviceName = "%sService"
        gc.serviceImplName = "%sServiceImpl"
        gc.controllerName = "%sController"
        mpg.globalConfig = gc
        //gc.setIdType(IdType.UUID);
        //是否开启Swagger2
        //gc.setSwagger2(true);
        //gc.setXmlName("%sDaoMapper");


        //数据源配置
        val dsc = DataSourceConfig()
        dsc.dbType = DbType.MYSQL
        dsc.url = url
        dsc.driverName = driverClass
        dsc.username = userName
        dsc.password = passWord
        mpg.dataSource = dsc


        //包配置
        val pc = PackageConfig()
        pc.parent = setParent
        pc.setMapper("dao")
                .setService("service")
                .setController("controller").entity = "entity"
        mpg.packageInfo = pc


        //自定义配置
        if (mapperPath.isNotEmpty()) {
            val cfg = object : InjectionConfig() {
                override fun initMap() {
                    // to do nothing
                }
            }
            val focList = ArrayList<FileOutConfig>()
            //mapper.xml
            focList.add(object : FileOutConfig("/templates/mapper.xml.ftl") {
                override fun outputFile(tableInfo: TableInfo): String {
                    // 自定义输入文件名称
                    return (projectPath + mapperPath
                            + tableInfo.mapperName + "Mapper" + StringPool.DOT_XML)
                }
            })
            cfg.fileOutConfigList = focList
            mpg.cfg = cfg
        }

        //策略配置
        val strategy = StrategyConfig()
        //下划线到驼峰的命名方式
        strategy.naming = NamingStrategy.underline_to_camel
        strategy.columnNaming = NamingStrategy.underline_to_camel
        //使用lombok
//        strategy.isEntityLombokModel = true
        strategy.isRestControllerStyle = true
        strategy.setInclude(scanner("表名"))
        strategy.isControllerMappingHyphenStyle = true
//        //继承自定义的controller，service，impl，dao
//        strategy.superControllerClass = "com.ywh.common.base.BaseController"
//        strategy.superServiceClass = "com.ywh.common.base.BaseService"
//        strategy.superServiceImplClass = "com.ywh.common.base.BaseServiceImpl"
//        strategy.superMapperClass = "com.ywh.common.base.BaseDao"
//        mpg.templateEngine = FreemarkerTemplateEngine()
        mpg.strategy = strategy
        mpg.execute()
    }
}